package com.inossem.warehousestorage_inossem.adapter.receive;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.receive.ReceiveAddReceiverActivity;
import com.inossem.warehousestorage_inossem.activity.receive.extra.ReceiveExtra;
import com.inossem.warehousestorage_inossem.activity.receive.ReceiveSelectReceiverActivity;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveReceiveItemBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.utils.ReceiveUtils;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class ReceiverListAdapter extends BaseAdapter {

    private Context mContext;
    private List<ReceiveReceiveItemBean.DataBean.ReceiverListBean> mReceiverList;
    private LayoutInflater inflater;
    private String mReceiveNumber, mPurchaseNumber, mPurchaseType, mSupplier;
    private ReceiveAddReceiverActivity.ReceiveAddReceiverListener mReceiveAddReceiverListener;
    private boolean mReadOnly;

    public ReceiverListAdapter(Context context, ReceiveReceiveItemBean.DataBean data, boolean readOnly, ReceiveAddReceiverActivity.ReceiveAddReceiverListener receiveAddReceiverListener) {
        mContext = context;
        mReceiveNumber = data.getReceiveNumber();
        mPurchaseNumber = data.getPurchaseNumber();
        mPurchaseType = data.getPurchaseType();
        mSupplier = data.getSupplier();
        mReceiverList = data.getReceiverList();
        mReadOnly = readOnly;
        mReceiveAddReceiverListener = receiveAddReceiverListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mReceiverList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mReceiverList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_add_receiver_list_first_item, null);
            TextView receiveNumber = (TextView) convertView.findViewById(R.id.receiveNumber);
            TextView purchase = (TextView) convertView.findViewById(R.id.purchase);
            TextView supplier = (TextView) convertView.findViewById(R.id.supplier);
            EditText condition = (EditText) convertView.findViewById(R.id.condition);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            receiveNumber.setText(mReceiveNumber);
            purchase.setText(mPurchaseNumber + " - " + mPurchaseType);
            supplier.setText(mSupplier);
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    String conditionStr = condition.getText().toString();
                    if ("".equals(conditionStr)) {
                        Utils.middleToast(mContext, mContext.getResources().getString(R.string.activity_receive_add_receiver_condition_null));
                        return;
                    }
                    Intent intent = new Intent(mContext, ReceiveSelectReceiverActivity.class);
                    intent.putExtra(ReceiveExtra.RECEIVER_CONDITION, conditionStr);
                    mContext.startActivity(intent);
                }
            });
            if (mReadOnly) {
                condition.setVisibility(View.GONE);
                search.setVisibility(View.GONE);
            }
        } else {
            ReceiveReceiveItemBean.DataBean.ReceiverListBean receiverItem = mReceiverList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_add_receiver_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, receiverItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.id = (TextView) view.findViewById(R.id.id);
        viewHolder.receiveStatus = (TextView) view.findViewById(R.id.receiveStatus);
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.receiveDate = (TextView) view.findViewById(R.id.receiveDate);
        viewHolder.department = (TextView) view.findViewById(R.id.department);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, ReceiveReceiveItemBean.DataBean.ReceiverListBean receiverItem) {
        viewHolder.id.setText(receiverItem.getId());
        viewHolder.receiveStatus.setTextColor(ContextCompat.getColor(context, R.color.activity_receive_manage_not_receive));
        viewHolder.receiveStatus.setText(receiverItem.getReceiveStatus());
        viewHolder.name.setText(receiverItem.getName());
        viewHolder.receiveDate.setText(receiverItem.getReceiveDate());
        viewHolder.department.setText(receiverItem.getDepartment());
        if (!mReadOnly) {
            view.setOnLongClickListener((View v) -> {
                DialogUtils.getConfirmDialog(mContext, "确认要删除 " + receiverItem.getName(), new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        ReceiveUtils.deleteReceiver(mContext, receiverItem.getId());
                        dialog.cancel();
                        mReceiveAddReceiverListener.onDeleteReceiverListener();
                    }
                }).show();
                return true;
            });
        }
    }

    private class ViewHolder {
        private TextView id, receiveStatus, name, receiveDate, department;
    }

}
